﻿/****---------------------------------------------------------------------------------------------------->>>>
*	使用工具：
*		flash pro 8.0  action script 2.0 
*
*	修改日记:
*		2007-08-07 - 张旸创建
*
*	详细记录:
*		类描述:
* 			- 实现当字数过长时间不显示在后面加三个点省略
*			- 实现原理为：AS创建一个fieldText对象来模拟
*
*			例子:
*				null
*
*			描述:
*				null
*
<<<<----------------------------------------------------------------------------------------------------****/

class mx.utils.TextDot{
	
	private static var filtrateStartArr=["&","<",">",'"',"'"];
	private static var filtrateEndArr=["&amp;","&lt;","&gt;","&quot;","&apos;"];
	
	//在字后面添加等号
	public static function addDot(txt,backText):TextField
	{
		if(backText==null){
			backText="... ";
		}
		var str = txt.text;
		trace(Math.ceil(txt.textWidth)+"---"+Math.ceil(txt._width))
		if(Math.ceil(txt.textWidth)>Math.ceil(txt._width)){
			var i:Number=1;
			txt.text="";
			while(txt.textWidth<txt._width) {
				txt.text=str.substring(0,i)+backText;
				i++
			}
		}else{
		}
		return txt
	}
	//过滤以下特殊符号不用HTML解析
	public static function filtrateHtml(str:String):String
	{
		var strResult:String=str;
		for(var i=0;i<filtrateStartArr.length;i++){
			strResult=displaceStr(strResult,filtrateStartArr[i],filtrateEndArr[i]);
		}
		return strResult;
	}
	private static function displaceStr(str:String,startStr:String,endStr:String){
		var arr=str.split(startStr);
		return arr.join(endStr);
	}
	//自动过滤复制的回车
	public static function clearEnter(txt:String)
	{
		var arr=txt.split("\r");
		var str="";
		for(var i=0;i<arr.length;i++){
			str+=arr[i];
		}
		return str;
	}
	//根据字数进行判断maxLength为单字节个数
	public static function getParseText(text:String,maxLength:Number):Number
	{
		if (text.length * 2 < maxLength) {
			return text.length;
		}
		var str = text;
		var num = 0;
		for (var i = 0; i < maxLength; i++) {
			if ((num + 2) > maxLength&&str.charCodeAt(i) > 256) {
				return i;
			}
			if (str.charCodeAt(i) > 256) {
				num += 2;
			}else {
				num++;
			}
		}
		return i;
	}
}